<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/10/19
 * Time: 16:16
 */

namespace app\index\table;

use app\index\controller\Login;
use app\index\service\ExcelService;
use think\Db;

class InvestTable extends BaseTable
{
    protected static $view = 'V_INVEST_INFO';
    protected static $table = 'INVEST_INFO';
    protected static $pk = 'INVEST_NO';
    protected static $configTable = 'INVEST_CONFIG';

    static public function table(){
        return Db::table(static::$view);
    }

    static public function add($data){
        $data['INVEST_NO'] = static::getNextId();
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');
        self::toDate($data,'BUY_DATE');
        self::toDate($data,'END_DATE');
        self::toDate($data,'INITIAL_DATE');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }
    static public function addImport($data){
        $data['INPUT_USER_ID'] = Login::getUserId();
        $data['INPUT_TIME'] = date('Y-m-d');
        self::toDate($data,'BUY_DATE');
        self::toDate($data,'END_DATE');
        self::toDate($data,'INITIAL_DATE');
        self::toDate($data,'INPUT_TIME');
        Db::table(static::$table)->insert($data);
    }

    static public function update($where,$data){
        self::toDate($data,'BUY_DATE');
        self::toDate($data,'END_DATE');
        self::toDate($data,'INITIAL_DATE');
        Db::table(static::$table)->where($where)->update($data);
    }

    static public function delete($where){
        return Db::table(static::$table)->where($where)->delete();
    }

    public static function getConfigAdd(){
        return Db::table(static::$configTable)->where(['ADD_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigEdit(){
        return Db::table(static::$configTable)->where(['EDIT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getConfigPrint(){
        return Db::table(static::$configTable)->where(['PRINT_FLAG'=>'1'])->order('ID ASC')->select();
    }

    public static function getNextId(){
        $year = date('Y');
        $numMaxRecord = Db::table(static::$table)
            ->field('max(SUBSTR(INVEST_NO,5,4)) as NUM')
            ->where([['SUBSTR(INVEST_NO,1,4)','=',$year]])
            ->fetchSql(true)
            ->find();
        $numMaxRecord = Db::query($numMaxRecord);
        $num = $numMaxRecord[0]['NUM'];
        if ($num == NULL) {
            $num = 0;
        }
        $num++;
        return $year.sprintf("%04d", $num);
    }

    public static function exportExcel($data,$fileName){

        $fieldList = [
            ['title'=>'编号','field'=>'INVEST_NO'],
            ['title'=>'被投资单位（项目）名称','field'=>'INVEST_NAME'],
            ['title'=>'期末账面数','field'=>'FINAL_ACCOUNT'],
            ['title'=>'投资性质','field'=>'INVEST_TYPE'],
            ['title'=>'购买日期','field'=>'BUY_DATE'],
            ['title'=>'到期日','field'=>'END_DATE'],
            ['title'=>'投资金额','field'=>'INVEST_FEE'],
            ['title'=>'票面金额','field'=>'PAR_FEE'],
            ['title'=>'初始投资日期','field'=>'INITIAL_DATE'],
            ['title'=>'累计投资额','field'=>'ACC_FEE'],
            ['title'=>'初始投资额','field'=>'INITIAL_FEE'],
            ['title'=>'追加投资额','field'=>'ADD_FEE'],
            ['title'=>'已处置投资额','field'=>'DEAL_FEE'],
            ['title'=>'持股比例（%）','field'=>'STOCK_SHARE'],
            ['title'=>'是否实际控制','field'=>'IS_CONTROL'],
            ['title'=>'组织形式','field'=>'ORGANIZATION_FORM'],
            ['title'=>'是否上市','field'=>'IS_LISTED'],
            ['title'=>'实收资本','field'=>'CAPITAL_PAID'],
            ['title'=>'资本公积','field'=>'CAPITAL_RESERVE'],
            ['title'=>'盈余公积','field'=>'EARNED_RESERVE'],
            ['title'=>'未分配利润','field'=>'RETAINED_BALANCE'],
            ['title'=>'当年投资收益','field'=>'YEAR_INCOME'],
            ['title'=>'审批情况','field'=>'STATUS'],
            ['title'=>'批准文号','field'=>'APPROVAL_NO'],
            ['title'=>'备注','field'=>'REMARK'],
            ['title'=>'制单人','field'=>'INPUT_NAME'],
            ['title'=>'制单时间','field'=>'INPUT_TIME'],
        ];

        $excel = new ExcelService();
        $excel->exportExcel($data,$fileName,$fieldList);
    }

    const INVEST_FIELD_LIST = [
        "INVEST_NO","INVEST_NAME","FINAL_ACCOUNT","INVEST_TYPE_ID","BUY_DATE","END_DATE","INVEST_FEE","PAR_FEE","INITIAL_DATE","ACC_FEE","INITIAL_FEE","ADD_FEE","DEAL_FEE","STOCK_SHARE","IS_CONTROL","ORGANIZATION_FORM","IS_LISTED","CAPITAL_PAID","CAPITAL_RESERVE","EARNED_RESERVE","RETAINED_BALANCE","YEAR_INCOME","AUDIT_STATUS_ID","APPROVAL_NO","REMARK"
    ];

    public static function getSessionConditions() {
        $condition = null;
        $listCondition = Login::getListConditionInvest();
        if (!empty($listCondition['INVEST_NO'])) {
            $value = $listCondition['INVEST_NO'];
            $condition[] = ['INVEST_NO','like',"$value%"];
        }
        if (!empty($listCondition['INVEST_NAME'])) {
            $value = $listCondition['INVEST_NAME'];
            $condition[] = ['INVEST_NAME','like',"%$value%"];
        }
        if (!empty($listCondition['INVEST_TYPE_ID'])) {
            $value = $listCondition['INVEST_TYPE_ID'];
            $condition[] = ['INVEST_TYPE_ID','=',$value];
        }
        if (!empty($listCondition['AUDIT_STATUS_ID'])) {
            $value = $listCondition['AUDIT_STATUS_ID'];
            $condition[] = ['AUDIT_STATUS_ID','=',$value];
        }
        if (!empty($listCondition['INVEST_FEE_START'])) {
            $value = $listCondition['INVEST_FEE_START'];
            $condition[] = ['INVEST_FEE','>=',$value];
        }
        if (!empty($listCondition['INVEST_FEE_END'])) {
            $value = $listCondition['INVEST_FEE_END'];
            $condition[] = ['INVEST_FEE','<=',$value];
        }
        return $condition;
    }
}